package com.xcx.minservice.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xcx.commonuntils.returnuntls.ErrorEnum;
import com.xcx.commonuntils.returnuntls.ReturnMessage;
import com.xcx.commonuntils.returnuntls.ReturnMessageUtils;

import com.xcx.minservice.entity.FileSystem;
import com.xcx.minservice.mapper.MinioMapper;
import com.xcx.minservice.request.GetUserFilesRequest;
import com.xcx.minservice.service.MinioService;
import com.xcx.minservice.untils.MinioUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Service
@Primary
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MinioServiceImpl extends ServiceImpl<MinioMapper, FileSystem> implements MinioService {


    final private MinioUtil minioUtil;
    final private MinioMapper minioMapper;


    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public ReturnMessage<Object> upload(MultipartFile file,String userId,Integer source) {

        try {
            FileSystem fileSystem = new FileSystem();
            fileSystem.setUserId(userId);
            String fileName = file.getOriginalFilename();
            String extendFileName = fileName.substring(fileName.lastIndexOf('.') + 1);
            String upload = minioUtil.upload(file);
            if ("该文件类型不能上传！".equals(upload)) {
                return ReturnMessageUtils.error(-2, "该文件类型不能上传！");
            }

            fileSystem.setFileName(fileName);
            fileSystem.setFileExt(extendFileName);
            fileSystem.setFilePath(upload);
            fileSystem.setSource(source);
            minioMapper.insert(fileSystem);
            return ReturnMessageUtils.success();
        } catch (Exception e) {
            log.error("e:{}", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ReturnMessageUtils.errorWithException(e);
        }
    }

    @Override
    public ReturnMessage<Object> preview(String fileName) {
        String preview = minioUtil.preview(fileName);

        return ReturnMessageUtils.success(preview);
    }

    @Override
    public void download(String fileName, HttpServletResponse res) {
        minioUtil.download(fileName,res);

    }

    @Override
    public ReturnMessage<Object> remove(String fileName) {


        boolean remove = minioUtil.remove(fileName);
        if (remove) {

            return ReturnMessageUtils.success();
        }
        return ReturnMessageUtils.errorWithEum(ErrorEnum.E_FILE_REMOVE_FAIL);
    }

    @Override
    public ReturnMessage<List<String>> uploads(MultipartFile[] files) {
        //判断上传文件是否为空
        if (null == files || files.length == 0) {

            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_FILE_UPLOADS_FAIL);
        }
        List<String> uploads = minioUtil.uploads(files);

        return ReturnMessageUtils.success(uploads);
    }

    @Override
    public ReturnMessage<List<FileSystem>> getUserFiles(GetUserFilesRequest request) {
        QueryWrapper<FileSystem> fileQueryWrapper = new QueryWrapper<>();
        fileQueryWrapper.eq("user_id",request.getId());
        List<FileSystem> fileSystems = minioMapper.selectList(fileQueryWrapper);
        return ReturnMessageUtils.success(fileSystems);
    }
}
